<?php

/**
 *web的入口
 */
namespace app\home\controller;
use app\comm\service\SrotGoodServices;
use app\comm\service\UserOrderService;
use app\comm\service\CityService;
use \think\Db;
use \think\Controller;
use app\comm\controller\PcLogController;
use ServS\Map\BaidDuMap; 
use think\Cookie;

/**
 * 网站入口控制器
 */
const LABEL_FONT_PATH_DEFAULT = '';
class Index extends PcLogController{

    protected $srogood;

    public function __autoindex(){
        $this->srogood = new SrotGoodServices;
    }
    
    /**
     * 网站入口
     */
    public function index($la=120.4385574309,$ln=36.076404325348) {
       $cityservice = new CityService();
       $citylist = $cityservice->getCityListByCache();
       $this->assign('citylist',$citylist);
       $this->assign('citylistjson',json_encode($citylist));
       $city = request()->get("city");
       if(!empty($city)){
           $int  =  900000;
           Cookie::set("city",$city,time()+$int);
       }
       if(Cookie::has("city")){
          $city = Cookie::get("city");   
       }
       $list =   $this->srogood->storlist($la,$ln,$city);
       $this->assign('list',$list);
       return view('index/index');  
    }

    /**
     * 定位
     */
    public function dinwei(){
        return view('index/dinwei');
    }

    /**
     * 定位获取地理信息
     */
    public function wezhi(){
        $baidumap = new BaidDuMap; 
        $rwel =$baidumap ->getbylatlnt(35.948726,120.153084);
        return json($rwel);
    }
    
    /**
     * 进入门店
     */
    public function store(){
        $id = request()->get("id");
        // $page =   $this->request ->get('page','1','');
        // $list =   $this->srogood->getstorgoods($id,$page=1);
        // return json($list);
       
        $list =[];
        return view('index/store',['list'=>$list,'storeid'=>$id]);    
    }

    /**
     * 获取店铺商品，ajax
     */
    public function goodscatage(){
         $list =   $this->srogood->getcatagstorgoods();
         return json($list);
    }

    /**
     * 搜索
     */
    public function serch(){
        $page=request()->get("page");
        $limit=request()->get("limit");
        $msg =request()->get("msg");
        $id  =request()->get("id");
        $catge=request()->get("catge");
        $store_id=request()->get("storeid");;
        $map["store_id"]=["eq",$store_id];
        if($catge!=""){
            $map["cat_id"]=["eq",$catge];
        }
        if($msg!=''){
           $map["name"]=["like","%".$msg."%"];
        }
        $resutl = Db::table("ys_view_storgood")->where($map)->page($page,$limit)->select();
        foreach ($resutl as $key => &$value) {
            $value["detail"]=htmlspecialchars_decode($value["detail"]);
        }
        return json($resutl);  
    }

    /**
     * 商品详情,获取商品的id
     * @return [type] [description]
     */
    public function goods(){
        $goodspecname = new \app\index\service\GoodSpec();
        $id = request()->get("id");
        $storeid         =   $this->request->get('storeid');
        $map["goods_id"] = ["eq",$id];
         //属性
        $goodspace = Db::table("ys_goods_spec")->where($map)->group('spec_id')->select();
        foreach ($goodspace as $key => &$value) {
           $value["spec"]     =   $goodspecname ->goodspec($value["spec_id"],$id); 
           $value["specname"] =   $goodspecname ->getys_goods_spec_name($value["spec_id"]);  
        }

        $goods = Db::table("ys_goods")->where("id",$id)->find();

        //没有属性的商品 价格与kucun 
        if($goods["is_spec"]=="0"){
            $goodspec = Db::table("ys_goods_spec_detail")->where("goods_id",$id)->find(); 
            $this->assign('goodspec',$goodspec); 
            $goodstoresock = Db::table("ys_store_goods_stock")->where("goods_id",$id)->find(); 
            //dd($goodspec);
            $this->assign('goodstoresock',$goodstoresock); 
        }
        $this->assign('stock',9999);
        $this->assign('goods',$goods);
        $this->assign('storeid',$storeid);
        $this->assign('goodspace',$goodspace);
        return view('index/goods'); 
    }

    /**
     * ajax   （商品库存）
     * 获取商品的库存，商品的数量
     */
    public  function getgoodsajax(){
        $specid = request()->get("param"); 
        $ary  = explode(",",$specid);
        //升续
        asort($ary);
        $str  =  implode(",",$ary);
        $stor_good_stock_map["specs"] = ["eq",$str];
        $store_goods_stock  =  Db::table("ys_store_goods_stock")->where($stor_good_stock_map)->find();   
        $spec_good_detai_map["specs"] = ["eq",$str];
        $goods_spec_detail  =  Db::table("ys_goods_spec_detail")->where($spec_good_detai_map)->find(); 
        $data["storegoodsstock"]  =  $store_goods_stock;
        $data["goodsspecdetail"]  =  $goods_spec_detail;
        return json(["status"=>1,"data"=>$data]); 
    }

    //商品属性api
    public function goodsapi(){
         $goodspecname = new \app\index\service\GoodSpec();
         $id = request()->get("id");
         //属性有两种
         $map["goods_id"] = ["eq",$id];
         $goodspec = Db::table("ys_goods_spec")->where($map)->group('spec_id')->select();
         foreach ($goodspec as $key => &$value) {
              $value["spec"]     =   $goodspecname ->goodspec($value["spec_id"],$id); 
              $value["specname"] =   $goodspecname ->getys_goods_spec_name($value["spec_id"]);  
         }
         //商品
         $goods = Db::table("ys_goods")->where("id",$id)->find();
         return json(["goodspeci" => $goodspec,"goods" => $goods]);
         //return json(["status"=>1,"data"=>$goods]);
    }

    //利用商品属性 获取商品的库存等
    public function goodsstoreapibyspec(){
        $msg = request()->get("msg");
        $map["specs"] = ["eq",$msg];
        $goodspec = Db::table("ys_goods_spec_detail")->where($map)->find();
        //$goodskucun = Db::table("ys_store_goods_stock")->where("id")->find();
        return  $goodspec;
    }

    /**
     * 获取附近的门店
     */
    public function getstor(){
        $la = request()->get("la");
        $ln = request()->get("ln");
        $list =   $this->srogood->storlist($la,$ln);  
        return  json($list);
    }

    /**
     * 订单回复您机制
     */
    public function orderrollback(){
        $userorderservice = new UserOrderService;
        $ord = $userorderservice->orderrollback();
    }

    /**
     * 订单测试
     * @return [type] [description]
     */
    public function test(){
    
    }
}

